﻿namespace TestTool.GUI.Controllers
{
    using System;
    using TestTool.GUI.Data;
    using TestTool.GUI.Data.Log;

    internal static class ContextController
    {
        private static readonly GeneralContext _context = new GeneralContext();

        public static ApplicationInfo GetApplicationInfo()
        {
            return _context.AppInfo;
        }

        public static DebugInfo GetDebugInfo()
        {
            return _context.DebugInfo;
        }

        public static DeviceEnvironment GetDeviceEnvironment()
        {
            return _context.Environment;
        }

        public static DiscoveredDevices GetDiscoveredDevices()
        {
            return _context.Devices;
        }

        public static ManagementSettings GetManagementSettings()
        {
            ManagementSettings settings = new ManagementSettings();
            DeviceEnvironment deviceEnvironment = GetDeviceEnvironment();
            TestSettings testSettings = deviceEnvironment.TestSettings;
            TestTool.GUI.Data.Log.Timeouts timeouts = new TestTool.GUI.Data.Log.Timeouts {
                MessageTimeout = deviceEnvironment.Timeouts.Message,
                OperationDelay = testSettings.OperationDelay,
                RebootTimeout = deviceEnvironment.Timeouts.Reboot,
                TimeBetweenRequests = testSettings.RecoveryDelay,
                TimeBetweenTests = deviceEnvironment.Timeouts.InterTests
            };
            settings.Timeouts = timeouts;
            Miscellaneous miscellaneous = new Miscellaneous {
                DefaultGatewayIpv4 = deviceEnvironment.EnvironmentSettings.GatewayIpv4,
                DefaultGatewayIpv6 = deviceEnvironment.EnvironmentSettings.GatewayIpv6,
                DnsIpv4 = deviceEnvironment.EnvironmentSettings.DnsIpv4,
                DnsIpv6 = deviceEnvironment.EnvironmentSettings.DnsIpv6,
                NtpIpv4 = deviceEnvironment.EnvironmentSettings.NtpIpv4,
                NtpIpv6 = deviceEnvironment.EnvironmentSettings.NtpIpv6,
                EventTopic = testSettings.EventTopic,
                MetadataFilter = testSettings.MetadataFilter,
                Password1 = testSettings.Password1,
                Password2 = testSettings.Password2,
                PTZNodeToken = testSettings.PTZNodeToken,
                RecordingToken = testSettings.RecordingToken,
                RelayOutputDelayTime = testSettings.RelayOutputDelayTimeMonostable,
                SearchTimeout = testSettings.SearchTimeout,
                SecureMethod = testSettings.SecureMethod,
                SubscriptionTimeout = testSettings.SubscriptionTimeout,
                TopicNamespaces = testSettings.TopicNamespaces,
                UseEmbeddedPassword = testSettings.UseEmbeddedPassword,
                RetentionTime = testSettings.RetentionTime
            };
            settings.Miscellaneous = miscellaneous;
            return settings;
        }

        public static MediaInfo GetMediaInfo()
        {
            return _context.MediaInfo;
        }

        public static PTZInfo GetPTZInfo()
        {
            return _context.PTZInfo;
        }

        public static RequestsInfo GetRequestsInfo()
        {
            return _context.RequestsInfo;
        }

        public static SetupInfo GetSetupInfo()
        {
            return _context.SetupInfo;
        }

        public static TestLog GetTestLog()
        {
            return _context.TestLog;
        }

        public static TestOptions GetTestOptions()
        {
            return _context.TestOptions;
        }

        public static void InitGeneralContext()
        {
            _context.AppInfo = new ApplicationInfo();
            _context.Devices = new DiscoveredDevices();
            _context.Environment = new DeviceEnvironment();
            _context.SetupInfo = new SetupInfo();
            _context.TestOptions = new TestOptions();
            _context.TestLog = new TestLog();
            _context.MediaInfo = new MediaInfo();
            _context.PTZInfo = new PTZInfo();
            _context.DebugInfo = new DebugInfo();
        }

        public static void UpdateDebugInfo(DebugInfo info)
        {
            _context.DebugInfo = info;
        }

        public static void UpdateDeviceEnvironment(DeviceEnvironment environment)
        {
            _context.Environment = environment;
        }

        public static void UpdateDiscoveredDevices(DiscoveredDevices devices)
        {
            _context.Devices = devices;
        }

        public static void UpdateMediaInfo(MediaInfo info)
        {
            _context.MediaInfo = info;
        }

        public static void UpdatePTZInfo(PTZInfo info)
        {
            _context.PTZInfo = info;
        }

        public static void UpdateRequestsInfo(RequestsInfo info)
        {
            _context.RequestsInfo = info;
        }

        public static void UpdateSetupInfo(SetupInfo info)
        {
            _context.SetupInfo = info;
        }

        public static void UpdateTestLog(TestLog log)
        {
            _context.TestLog = log;
        }

        public static void UpdateTestOptions(TestOptions options)
        {
            _context.TestOptions = options;
        }

        public static GeneralContext Context
        {
            get
            {
                return _context;
            }
        }
    }
}

